//
//  RootViewController.m
//  UI06_作业（轮播图）
//
//  Created by dllo on 15/10/17.
//  Copyright (c) 2015年 Ljd. All rights reserved.
//

#import "RootViewController.h"

#define WIDTH     [[UIScreen mainScreen] bounds].size.width
#define HEIGHT    [[UIScreen mainScreen] bounds].size.height


@interface RootViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollV;

@property (nonatomic, retain) UIScrollView *smallScrollV;

@property (nonatomic, retain) UIPageControl *pageC;

@property (nonatomic, retain) UIScrollView *saveSrollV;


@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建大 scrollView
    self.scrollV = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.scrollV.backgroundColor = [UIColor greenColor];
    self.scrollV.delegate = self;
    self.scrollV.bounces = NO;
    self.scrollV.contentSize = CGSizeMake(WIDTH * 10, HEIGHT);
    self.scrollV.pagingEnabled = YES;
    
    
    // 从第二张图开始
    self.scrollV.contentOffset = CGPointMake(WIDTH, 0);
    [self.view addSubview:self.scrollV];
    [self.scrollV release];
    
    
    // 图片数组
    NSMutableArray *imageArr = [NSMutableArray arrayWithObjects:@"8.jpg", @"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"1.jpg", nil];
    
    // 将图片添加到小scrollView
    for (int i = 0; i < 10; i++) {
        
        self.smallScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        self.smallScrollV.backgroundColor = [UIColor cyanColor];
        [self.scrollV addSubview:self.smallScrollV];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        imageV.image = [UIImage imageNamed:[imageArr objectAtIndex:i]];
        
        // 实现缩放
        self.smallScrollV.delegate =self;
        self.smallScrollV.maximumZoomScale = 3.0;
        self.smallScrollV.minimumZoomScale = 0.3;

        [self.smallScrollV addSubview:imageV];
        [self.smallScrollV release];
        [imageV release];
        
    }
    
    // 创建pagControl
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake((WIDTH - 200) / 2, HEIGHT - 100, 200, 50)];
//    self.pageC.backgroundColor = [UIColor yellowColor];
    self.pageC.numberOfPages = 8;
    self.pageC.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageC.pageIndicatorTintColor = [UIColor whiteColor];
    [self.pageC addTarget:self action:@selector(pageCAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.pageC];
    [self.pageC release];
    
    
 
    
}

// 实现缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    // 将传进来的当前小scroView暂时存放在saveScrollV中
    self.saveSrollV = scrollView;
    return [scrollView.subviews objectAtIndex:0];
    
    
}

// 缩放结束，使视图居中
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    UIImageView *imageView = [scrollView.subviews objectAtIndex:0];
    imageView.center = CGPointMake(WIDTH / 2, HEIGHT / 2);
}

// 按点同步照片
- (void)pageCAction:(UIPageControl *)pageC
{
    [self.scrollV setContentOffset:CGPointMake(WIDTH * (pageC.currentPage + 1), 0) animated:YES];
}


// 滑动图片同步点
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"将要停止");
    // 循环拖动
    if (self.scrollV.contentOffset.x == WIDTH * 9) {
        
        self.scrollV.contentOffset = CGPointMake(WIDTH , 0);
        self.pageC.currentPage = 0;
        
    } else if (self.scrollV.contentOffset.x == WIDTH * 0)
    {
        self.scrollV.contentOffset = CGPointMake(WIDTH * 8, 0);
        self.pageC.currentPage = 8;
    }
    self.pageC.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    
    // 还原尺寸（在拖拽将要结束的时候实现）
    self.saveSrollV.zoomScale = 1;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
